package com.yycx.module.bus.provider.handler.settle;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenUser;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.mapper.BaseUserDeptMapper;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 结算单分页列表扩展器
 *
 * @author zyf
 */
@Component("settlePaymentPageListHandler")
public class SettlePaymentPageListHandler implements PageInterceptor<BusPayment> {
    @Resource
    private TaskService taskService;
    @Resource
    private BusOrderDetailsService orderDetailsService;
    @Resource
    private BusCustomService customService;
    @Resource
    private BaseUserService baseUserService;
    @Autowired
    private BaseUserDeptMapper userDeptMapper;

    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        String paymentNo = params.get("orderNo");
        String paymentState = params.get("paymentState");
        Integer auth = cq.getInt("auth", 0);
        Long deptId = params.getLong("deptId");
        OpenUser openUser = OpenHelper.getUser();
        cq.select(BusPayment.class, "*");
        //查询部分负责
        cq.addSelect("deptSaleMan.saleManName deptManagerName");
        cq.addSelect(BusSalemanBank.class, "bankName", "bankNo");
        cq.addSelect("saleman.saleManName");
        cq.addSelect(BusDept.class, "deptName");
        cq.addSelect(BaseUser.class, "nickName");
        cq.like("payment.paymentNo", paymentNo);

        if (auth.equals(CommonConstants.INT_1)) {
            Long userId = openUser.getUserId();
            List<Long> deptIds = userDeptMapper.selectUserDeptIds(userId);
            if (FlymeUtils.isNotEmpty(deptIds) && FlymeUtils.isEmpty(deptId)) {
                cq.in("payment.deptId", deptIds);
            } else {
                cq.eq(BusPayment.class, "deptId");
            }
        }
        cq.in(true, "amountType", "Settle");
        cq.eq(true, "payment.companyId", OpenHelper.getCompanyId());
        if (FlymeUtils.isNotEmpty(paymentState)) {
            cq.in("payment.paymentState", paymentState);
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge(BusPayment.class, "payDate", beginDate);
        }
        if (FlymeUtils.isNotEmpty(endDate)) {
            cq.le(BusPayment.class, "payDate", endDate);
        }
        cq.createJoin(BusDept.class);
        cq.createJoin(BusSalemanBank.class);
        cq.createJoin(BusSaleman.class).setMainAlias("dept").setMainField("saleManId").setJoinAlias("deptSaleMan").setJoinField("saleManId");
        //通过收款人账户查询收款人姓名
        cq.createJoin(BusSaleman.class).setMainAlias("bsb").setMainField("saleManId").setJoinField("saleManId");
        cq.createJoin(BaseUser.class);
        cq.orderByDesc("payment.createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            Long customId = entityMap.getLong("customId");
            if (FlymeUtils.isNotEmpty(customId)) {
                BusCustom custom = customService.getById(customId);
                entityMap.put("customName", custom.getCustomName());
            } else {
                entityMap.put("customName", "批量结算");
            }
            Long payUserId = entityMap.getLong("payUserId");
            BaseUser payUser = baseUserService.getUserById(payUserId);
            if (FlymeUtils.isNotEmpty(payUser)) {
                String payUserName = payUser.getNickName();
                entityMap.put("payUserName", payUserName);
            }
            if (FlymeUtils.isNotEmpty(processInstanceId)) {
                //查询当前审批节点
                Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
                if (FlymeUtils.isNotEmpty(task)) {
                    entityMap.put("taskName", task.getName());
                    entityMap.put("taskId", task.getId());
                    entityMap.put("taskDefinitionKey", task.getTaskDefinitionKey());
                }
            }
        }
        return result;
    }
}
